package com.fileCenter.dao.impl;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.fileCenter.dao.BaseDAO;
import com.fileCenter.dao.ModelDAO;
import com.fileCenter.exception.FileCenterException;
import com.fileCenter.pojo.File;
import com.fileCenter.pojo.Model;
import com.fileCenter.util.HQLUtil;

public class ModelDAOImpl extends BaseDAO implements ModelDAO {

	private static String queryByNameHQL = "from Model m where m.name=:name";
	private static String getByIdsHQL = "from Model m where m.id in (:ids)";;
	
	@SuppressWarnings("unchecked")
	@Override
	public Model queryByName(String name) throws FileCenterException {
		List<Model> list = getHibernateTemplate().findByNamedParam(queryByNameHQL, "name", name);
		return (Model)(list.isEmpty() ? null : list.get(0));
	}

	@Override
	public Model queryById(long userId) throws FileCenterException {
		return (Model) getHibernateTemplate().get(Model.class, userId);
	}

	@Override
	public void update(Model model) throws FileCenterException {
		getHibernateTemplate().update(model);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Model> queryByAttr(Map<String, Object> attrNameAndValueMap)
			throws FileCenterException {
		String queryByAttrHQL = "";
		List<Model> list = Collections.emptyList();
		String[] paramNames = null;
		Object[] paramValues = null;
		if (MapUtils.isNotEmpty(attrNameAndValueMap)) {
			queryByAttrHQL = HQLUtil.generateQueryByAttrHQL(Model.class, attrNameAndValueMap.keySet());
			paramNames = new String[attrNameAndValueMap.size()];
			paramValues = new Object[attrNameAndValueMap.size()];
			
			Set<String> keySet = attrNameAndValueMap.keySet();
			Iterator<String> it = keySet.iterator();
			for (int i=0;it.hasNext();i++) {
				paramNames[i] = it.next();
				paramValues[i] = attrNameAndValueMap.get(paramNames[i]);
			}
		} else {
			queryByAttrHQL = HQLUtil.generateQueryByAttrHQL(Model.class, null);
		}
		
		if (paramNames != null && paramValues != null) {
			list = getHibernateTemplate().findByNamedParam(queryByAttrHQL, paramNames, paramValues);
		} else {
			list = getHibernateTemplate().find(queryByAttrHQL);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Model> getByIds(List<Long> ids) {
		if (CollectionUtils.isEmpty(ids))
			return Collections.emptyList();
		
		final List<Long> localIds = ids;
		List<Model> list = (List<Model>) getHibernateTemplate().execute(
		new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(getByIdsHQL);
				query.setParameterList("ids", localIds);
				return query.list();
			}
		});
		return (List<Model>) (CollectionUtils.isEmpty(list) ? 
				Collections.emptyList() : list);
	}
}
